package com.xtravel.tweet.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lidroid.xutils.BitmapUtils;
import com.xtravel.R;
import com.xtravel.common.adapter.BaseDataAdapter;
import com.xtravel.common.widget.TweetImageView;
import com.xtravel.tweet.database.dto.AppLive;
import com.xtravel.tweet.database.dto.AppUser;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class DestinationTravelLiveListAdapter extends BaseDataAdapter<AppLive> {
    private final BitmapUtils bitmapUtils;
    private int imgWidth = 480;
    private int imgHeight = 480;
    private int tweetPosition;
    private final Context context;
    private LayoutInflater mInflater;

    public DestinationTravelLiveListAdapter(Context context, List<AppLive> dataList) {
        super(dataList);
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.bitmapUtils = new BitmapUtils(context);;

        WindowManager wm = ((Activity) context).getWindowManager();
        DisplayMetrics dm = new DisplayMetrics();
        int width = wm.getDefaultDisplay().getWidth();
        float density = context.getResources().getDisplayMetrics().density;
        imgWidth = (int) (width - ((32) * density)) / 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.activity_tweet_detail_live, null);
            holder.imageView = (TweetImageView) convertView.findViewById(R.id.live_cover_img);
            holder.liveNameTextView = (TextView) convertView.findViewById(R.id.live_name);
            holder.liveAchorImg = (TweetImageView)convertView.findViewById(R.id.live_achor_img);
            holder.liveAchorName = (TextView)convertView.findViewById(R.id.live_achor_name);
            holder.liveViewerCount = (TextView)convertView.findViewById(R.id.live_viewer_count);
            holder.liveAchorLayout = (LinearLayout)convertView.findViewById(R.id.live_achor_layout);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final AppLive appLive = (AppLive) getItem(position);
        holder.liveNameTextView.setText(appLive.getName());
        // 加载网络图片
        holder.imageView.setImageUrl(bitmapUtils, appLive.cover);
        final AppUser achor = appLive.getAnchor();
        if(achor != null ){
            if(StringUtils.isNotEmpty(achor.getAvatarUri())) {
                holder.liveAchorImg.setImageUrl(bitmapUtils, achor.getAvatarUri());
            }
            if(StringUtils.isNotEmpty(achor.getAvatarUri())){
                holder.liveAchorName.setText(achor.getNickname());
            }
        }
        holder.liveViewerCount.setText(String.valueOf(appLive.getViewerCount() == null ? 0 : appLive.getViewerCount()));
/*        holder.liveAchorLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, OtherIndexActivity.class);
                intent.putExtra("id",achor.id);
                context.startActivity(intent);
            }
        });*/

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = null;
//                if (appLive.isLive) {
//                    intent = new Intent(context, ViewLiveMediaActivity.class);
//                } else {
//                    intent = new Intent(context, ViewRecordMediaActivity.class);
//                }
//                intent.putExtra("id", appLive.id);
//                intent.putExtra("liveUrl", appLive.liveUrl);
//                AppLocation location = appLive.getLocation();
//                intent.putExtra("address", location == null ? "" : location.address);
//                context.startActivity(intent);
            }
        });

        return convertView;
    }

    private void setRancle(View view) {
        LayoutParams lp = view.getLayoutParams();
        if (lp == null) {
            lp = new LayoutParams(imgWidth, imgHeight);
        }

        lp.width = imgWidth;
        lp.height = imgHeight;
        view.setLayoutParams(lp);
    }



    public final class ViewHolder {
        public TweetImageView imageView;
        public TextView liveNameTextView;
        public TweetImageView liveAchorImg;
        public TextView liveAchorName;
        public TextView liveViewerCount;
        public LinearLayout liveAchorLayout;
    }

    @Override
    public void getData(Object args) {

    }

    @Override
    protected void updateData(List<AppLive> data) {

    }
}